package test1;

import static org.junit.Assert.*;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Reader;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class TestIO {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void test1() throws Exception{
		InputStream in=new FileInputStream("data.txt");
		byte[] buf=new byte[in.available()];
		//byte b=(byte)in.read();
		//EOF(111111111111111111111111)
//		int b=0;
//		while((b=in.read())!=-1){
//			System.out.println(b);
//		}
		
//		int len=in.read(buf);
//		System.out.println(len);
//		for(byte b:buf){
//			System.out.println(b);
//		}
		in.read(buf);
		String s=new String(buf);
		System.out.println(s);
	}
	
	@Test
	public void test2() throws Exception{
		InputStream in=new FileInputStream("data1.txt");
		byte[] buf=new byte[in.available()];
//		byte b=(byte)in.read();
		in.read(buf);
		System.out.println(new String(buf,"UTF-16"));
		in.close();
	}
	
	@Test
	public void test3() throws Exception{
		OutputStream out=new FileOutputStream("data3.txt");
		out.write(97);
		out.write("abcdef中国字".getBytes("UTF-16"));
		
		out.flush();
		out.close();
	}
	
	@Test
	public void test4() throws Exception{
		double m=23.45;
		OutputStream out=new FileOutputStream("data5.txt");
		DataOutputStream dout=new DataOutputStream(out);
		dout.writeLong(23456L);
		dout.writeUTF("Hello");
		dout.writeDouble(m);
		dout.close();
	}
	
	
	@Test
	public void test5() throws Exception{
		DataInputStream din=new DataInputStream(new FileInputStream("data5.txt"));
		double m=din.readDouble();
		din.close();
		System.out.println(m);
		
	}
	@Test
	public void test6() throws Exception{
		User u1=new User(1,"john123",100);
		ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("data.info"));
		out.writeObject(u1);
		out.close();
		
	}
	
	
	@Test
	public void test7() throws Exception{
		ObjectInputStream in=new ObjectInputStream(new FileInputStream("data.info"));
		User u=(User)in.readObject();
		System.out.println(u.getId()+"\t"+u.getName()+"\t"+u.getAge());
	}
	@Test
	public void test8() throws Exception{
		OutputStream out=new FileOutputStream("data123.txt");
		out.write("中国".getBytes("UTF-16LE"));
		out.close();
	}
	
	@Test
	public void test9() throws Exception{
		
		InputStream in=new FileInputStream("data235.txt");
		BufferedReader reader=new BufferedReader(new InputStreamReader(in, "UTF-16"));
		String line=null;
		while((line=reader.readLine())!=null){
			System.out.println(line);
		}
		
	}
	
	
	public static void main(String[] args) throws Exception {
		long start=System.currentTimeMillis();
//		InputStream in=new FileInputStream("cd.mp3");
//		OutputStream out=new FileOutputStream("cd1.mp3");
//		byte[] buf=new byte[256];
//		int len=0;
//		while((len=in.read(buf))!=-1){
//			out.write(buf,0,len);
//		}
//		in.close();
//		out.close();
		BufferedInputStream bin=new BufferedInputStream(new FileInputStream("cd.mp3"),256);
		BufferedOutputStream bout=new BufferedOutputStream(new FileOutputStream("cd2.mp3"),256);
		int m=0;
		while((m=bin.read())!=-1){
			bout.write(m);
		}
		bin.close();
		bout.close();
		
		
		
		long end=System.currentTimeMillis();
		System.out.println(end-start);
	}
	
	

}
